package amushen.module;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import amushen.base.BaseBO;
import amushen.base.DBUtil;

public class Assess extends BaseBO {

	/**
	 * return pending list
	 * @param paras
	 * @param user
	 * @param is
	 * @param db
	 * @return
	 */
	public Object pending(Map paras, String user, InputStream is,
			DBUtil db) {
		JSONArray arr=new JSONArray();		
		List list=db.query("Assess.pending", new Object[]{user});
		for(int i=0;i<list.size();i++){
			try{
				JSONObject obj=new JSONObject();
				Object[] row=(Object[])list.get(i);
				obj.put("id", row[0]);
				obj.put("assess_person", row[1]);
				obj.put("assessed_person", row[2]);
				obj.put("state", row[3]);
				arr.put(obj);
			}catch(JSONException jsone){
				jsone.printStackTrace();
			}
		}
		return arr.toString();
	}
	
	/**
	 * get alrady list
	 * @param paras
	 * @param user
	 * @param is
	 * @param db
	 * @return
	 */
	public Object already(Map paras, String user, InputStream is,
			DBUtil db) {
		JSONArray arr=new JSONArray();		
		List list=db.query("Assess.already", new Object[]{user});
		for(int i=0;i<list.size();i++){
			try{
				JSONObject obj=new JSONObject();
				Object[] row=(Object[])list.get(i);
				obj.put("id", row[0]);
				obj.put("assess_person", row[1]);
				obj.put("assessed_person", row[2]);
				obj.put("state", row[3]);
				arr.put(obj);
			}catch(JSONException jsone){
				jsone.printStackTrace();
			}
		}
		return arr.toString();
	}
	
	
	/**
	 * get questions
	 * @param paras person_relative's id 
	 * @param user
	 * @param is
	 * @param db
	 * @return
	 */
	public Object getQuestions(Map paras, String user, InputStream is, DBUtil db){
		System.out.println("ID:"+(String)paras.get("id"));
		List list=db.query("Assess.questions", new Object[]{getI((String)paras.get("id"))});
		JSONArray ret=new JSONArray();
		for(int i=0;i<list.size();i++){
			JSONObject o=new JSONObject();
			Object[] r=(Object[])list.get(i);
			try {
				o.put("person_relative_id", r[0]);
				o.put("question_id", r[1]);
				o.put("type", r[2]);
				o.put("content", r[3]);
				o.put("right_answer", r[4]);
				ret.put(o);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return ret.toString();
	}	
	
	
	/**
	 * submit answer
	 * @param paras
	 * @param user
	 * @param is
	 * @param db
	 * @return
	 */
	public Object answer(Map paras, String user, InputStream is, DBUtil db){
		//System.out.println(paras.get("answer"));
		String ans=(String)paras.get("answer");
		ans=ans.substring(1);
		Integer personRelativeId=new Integer(0);
		String[] answers=ans.split(",");
		String[] answer;
		boolean flag=true;
		int ret=0;
		
		for(int i=0;i<answers.length;i++){
			//System.out.print(answers[i]);
			answer=answers[i].split("\\_");
			//System.out.println(",len:"+answer.length);
			if(flag){
				//first run need to delete same answers
				flag=false;
				personRelativeId=new Integer(answer[1]);
				ret=db.update("Assess.delAnswer", new Object[]{personRelativeId});
				//ret=db.update("Assess.reAnswer", new Object[]{personRelativeId});				
				if(ret<0){
					db.rollback();
					return "Can't delete old answers";
				}
			}			
			ret=db.update("Assess.answer3", new Object[]{new Integer(answer[1]),
					new Integer(answer[2]),new Integer(answer[3])});
			if(ret<0){
				db.rollback();
				return "Can't insert answer.";
			}
			
		}
		ret=db.update("Assess.answerOK", new Object[]{personRelativeId});
		if(ret<0){
			db.rollback();
			return "Can't finish answer";
		}
		db.commit();
		return "Submited";
	}
	
	/**
	 * 
	 * @param paras
	 * @param user
	 * @param is
	 * @param db
	 * @return
	 */
	public Object queryOne(Map paras, String user, InputStream is, DBUtil db){
		String personRelativeId=(String)paras.get("person_relative_id");
		List list=db.query("Assess.queryOne", new Object[]{new Integer(personRelativeId)});
		JSONArray ret=new JSONArray();
		Object[] row;
		for(int i=0;i<list.size();i++){
			row=(Object[])list.get(i);
			JSONObject obj=new JSONObject();
			try {				
				obj.put("person_relative_id", row[0]);
				obj.put("question_id", row[1]);
				obj.put("type", row[2]);
				obj.put("No", row[3]);
				obj.put("content", row[4]);
				if(row[5]==null)
					obj.put("answer", "");
				else
					obj.put("answer", row[5]);
				ret.put(obj);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return ret.toString();
	}
	
	/**
	 * query assessed person list
	 * @param paras
	 * @param user
	 * @param is
	 * @param db
	 * @return
	 */
	public Object queryAssessedPerson(Map paras, String user, InputStream is, DBUtil db){
		JSONArray ret=new JSONArray();
		List list=db.query("Assess.queryAssessedPerson", new Object[]{});
		Object[] row;
		JSONObject o;
		for(int i=0;i<list.size();i++){
			row=(Object[])list.get(i);
			o=new JSONObject();
			try {
				o.put("data", row[0]);
				o.put("label", row[1]);
			} catch (JSONException e) {
				e.printStackTrace();
			}			
			ret.put(o);
		}		
		//System.out.println(ret.toString());
		return ret.toString();
	}
	
	/**
	 * query all persons assess one person
	 * @param paras
	 * @param user
	 * @param is
	 * @param db
	 * @return
	 */
	public Object queryAlltoOne(Map paras, String user, InputStream is, DBUtil db){
		String assessedPerson=(String)paras.get("assessed_person");		
		JSONArray ret=new JSONArray();
		if(assessedPerson!=null&&assessedPerson.length()>0){			
			List list=db.query("Assess.queryAlltoOne", new Object[]{assessedPerson});
			Object[] row;
			JSONObject o;
			for(int i=0;i<list.size();i++){
				row=(Object[])list.get(i);
				o=new JSONObject();
				try {
					o.put("assessPerson", row[0]);
					o.put("questionId", row[1]);
					o.put("questionNo", row[2]);
					o.put("score", row[3]);
					o.put("assessedPerson", row[4]);
					o.put("personRelativeId", row[5]);
				} catch (JSONException e) {
					e.printStackTrace();
				}			
				ret.put(o);
			}
		}		
		return ret.toString();
	}
	
	/**
	 * init database for sv and manager
	 * @param paras
	 * @param user
	 * @param is
	 * @param db
	 * @return
	 */
	public Object initData(Map paras, String user, InputStream is,
			DBUtil db) {
		try{
			int ret=-1;
			ret=db.update("Assess.deleteAnswers",new Object[]{});
			if(ret==-1){
				db.rollback();
				return "Fail deleteAnswers";
			}
			ret=db.update("Assess.deletePersonRelative",new Object[]{});
			if(ret==-1){
				db.rollback();
				return "Fail delete person relative";
			}
			ret=db.update("Assess.InitPersonRelativeStaffToSV",new Object[]{});
			if(ret==-1){
				db.rollback();
				return "Fail init for sa to sv";
			}
			ret=db.update("Assess.InitPersonRelativeSVToSV",new Object[]{});
			if(ret==-1){
				db.rollback();
				return "Fail init for sv2sv";
			}
			ret=db.update("Assess.InitPersonRelativeMGToSV",new Object[]{});
			if(ret==-1){
				db.rollback();
				return "Fail init for mg2sv";
			}
			ret=db.update("Assess.InitPersonRelativeSVToMG",new Object[]{});
			if(ret==-1){
				db.rollback();
				return "Fail init for sv2mg";
			}
			ret=db.update("Assess.InitPersonRelativeMGToMG",new Object[]{});
			if(ret==-1){
				db.rollback();
				return "Fail init for mg2mg";
			}
			ret=db.update("Assess.InitPersonRelativePTToMG",new Object[]{});
			if(ret==-1){
				db.rollback();
				return "Fail init for pt2mg";
			}
			db.commit();
		}catch(Exception e){
			e.printStackTrace();
			db.rollback();
			return "Failure";
		}
		
		return "Success";
	}
	
}
